package com.xique.door.project.miniprogram.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xique.common.core.utils.StringUtils;
import com.xique.door.bean.domain.ResourceBit;
import com.xique.door.bean.vo.advert.ResourceBitVo;
import com.xique.door.constant.ResourcePositionConstant;
import com.xique.door.mapper.ResourceBitMapper;
import com.xique.door.project.miniprogram.bean.vo.MiniResourceBitVO;
import com.xique.door.project.miniprogram.service.IMiniResourceBitService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MiniResourceBitServiceImpl extends ServiceImpl<ResourceBitMapper, ResourceBit>
        implements IMiniResourceBitService {

    @Override
    public List<MiniResourceBitVO> getResourceBitList(String resourceType, String position, String positionType) {
        QueryWrapper<ResourceBit> queryWrapper = new QueryWrapper<>();
        if (resourceType != null && !"".equals(resourceType)) {
            queryWrapper.eq("resource_type", Integer.valueOf(resourceType));
        }
        if (positionType != null && !"".equals(positionType)) {
            queryWrapper.eq("position_type", positionType);
        }
        queryWrapper.eq("position", position);
        queryWrapper.eq("status", "1");

        queryWrapper.le("put_starttime", new Date());
        if (!"8".equals(position)) {
            queryWrapper.ge("put_endtime", new Date());
        }
        queryWrapper.orderByAsc("picturesort");
        List<ResourceBit> list = baseMapper.selectList(queryWrapper);
        List<MiniResourceBitVO> voList = new ArrayList<>();
        if (list.size() > 0) {
            for (ResourceBit bit : list) {
                MiniResourceBitVO vo = new MiniResourceBitVO();
                vo.setId(bit.getId());
                vo.setPosition(bit.getPosition());
                vo.setUrl(bit.getUrl());
                vo.setPictureUrl(bit.getPictureUrl());
                vo.setExhibition(bit.getExhibition());
                vo.setFileType(bit.getFileType());
                vo.setTitle(bit.getTitle());
                voList.add(vo);
            }
        }
        return voList;
    }


    @Override
    public ResourceBitVo getPopup(String resourceType, String positionType) {
        LambdaQueryWrapper<ResourceBit> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ResourceBit::getPosition, ResourcePositionConstant.MINI_JUMP_POP);
        queryWrapper.eq(ResourceBit::getStatus, 1);
        queryWrapper.eq(ResourceBit::getResourceType,Integer.valueOf(resourceType));
        queryWrapper.eq(ResourceBit::getPositionType, positionType);
        queryWrapper.le(ResourceBit::getPutStarttime, new Date());
        queryWrapper.ge(ResourceBit::getPutEndtime, new Date());
        queryWrapper.orderByDesc(ResourceBit::getCreateTime).last("limit 1");
        ResourceBit resourceBit = baseMapper.selectOne(queryWrapper);
        ResourceBitVo resourceBitVo = new ResourceBitVo();
        if (resourceBit != null && resourceBit.getJumpDestination() != null) {
                resourceBitVo.setPictureUrl(StringUtils.isNotBlank(resourceBit.getPictureUrl()) ? resourceBit.getPictureUrl() : "");
                resourceBitVo.setUrl(StringUtils.isNotBlank(resourceBit.getUrl()) ? resourceBit.getUrl() : "");
                resourceBitVo.setAppId(StringUtils.isNotBlank(resourceBit.getAppId())? resourceBit.getAppId():"");
                resourceBitVo.setAppAddress(StringUtils.isNotBlank(resourceBit.getAppAddress())?resourceBit.getAppAddress():"");
                resourceBitVo.setJumpDestination(resourceBit.getJumpDestination());
        }
        return resourceBitVo;
    }
}
